/**
 * 反馈工具模块
 * 封装了常用的用户反馈方法，如 toast、modal 等
 */

/**
 * 显示轻提示
 * @param {string} title - 提示内容
 * @param {Object} options - 配置选项
 * @param {'success'|'error'|'loading'|'none'} options.icon - 图标类型
 * @param {number} options.duration - 显示时长(ms)
 * @param {boolean} options.mask - 是否显示透明蒙层
 * @param {string} options.position - 显示位置 ('top'|'center'|'bottom')
 * @returns {Promise} - 返回Promise对象
 */
export const showToast = (title, options = {}) => {
  const {
    icon = 'none',
    duration = 1500,
    mask = false,
    position = 'center'
  } = options;
  
  return new Promise((resolve) => {
    uni.showToast({
      title,
      icon,
      duration,
      mask,
      position,
      image: '', // 如果使用 image 属性，可以自定义图标
      success: resolve,
      fail: (err) => {
        console.error('showToast failed:', err);
        resolve();
      }
    });
  });
};

/**
 * 显示加载提示
 * @param {string} title - 提示内容，默认'加载中'
 * @param {boolean} mask - 是否显示透明蒙层，默认true
 * @returns {void}
 */
export const showLoading = (title = '加载中', mask = true) => {
  uni.showLoading({
    title,
    mask
  });
};

/**
 * 隐藏加载提示
 * @returns {void}
 */
export const hideLoading = () => {
  uni.hideLoading();
};

/**
 * 显示模态对话框
 * @param {string} title - 标题
 * @param {string} content - 内容
 * @param {Object} options - 配置选项
 * @param {boolean} options.showCancel - 是否显示取消按钮
 * @param {string} options.confirmText - 确认按钮文字
 * @param {string} options.cancelText - 取消按钮文字
 * @returns {Promise<boolean>} - 返回用户选择结果(true为确认，false为取消)
 */
export const showModal = (title, content, options = {}) => {
  const {
    showCancel = true,
    confirmText = '确定',
    cancelText = '取消'
  } = options;

  return new Promise((resolve) => {
    uni.showModal({
      title,
      content,
      showCancel,
      confirmText,
      cancelText,
      success: (res) => {
        resolve(res.confirm);
      },
      fail: () => {
        resolve(false);
      }
    });
  });
};

/**
 * 显示成功提示
 * @param {string} title - 提示内容
 * @param {number} duration - 显示时长(ms)
 * @returns {Promise}
 */
export const showSuccess = (title, duration = 1500) => {
  return showToast(title, { icon: 'success', duration });
};

/**
 * 显示错误提示
 * @param {string} title - 提示内容
 * @param {number} duration - 显示时长(ms)
 * @returns {Promise}
 */
export const showError = (title, duration = 1500) => {
  return showToast(title, { icon: 'error', duration });
};

/**
 * 显示警告提示
 * @param {string} title - 提示内容
 * @param {number} duration - 显示时长(ms)
 * @returns {Promise}
 */
export const showWarning = (title, duration = 2000) => {
  return showToast(title, { 
    icon: 'none', 
    duration,
    position: 'top'
  });
};

// 导出所有方法
export default {
  showToast,
  showLoading,
  hideLoading,
  showModal,
  showSuccess,
  showError,
  showWarning
};